package com.springcloud.sc.ucpv5.client.resource;

import java.util.HashMap;
import java.util.Map;

import com.springcloud.sc.ucpv5.client.application.util.Constant;

public class ResourceDataManager {
	private static ResourceDataManager instance;
	private Map<String, ResourceManager> resourceMap = new HashMap<String, ResourceManager>();

	public static ResourceDataManager getInstance() {
		if (instance == null) {
			instance = new ResourceDataManager();
		}
		return instance;
	}

	private ResourceDataManager() {
		init();
	}

	public void init() {
		try {
			{
				ResourceManager resMgr = ResourceManager.getInstance(Constant.MODULE_FRAMEWORK_ID, Constant.FRAMEWORK_RESOURCE);
				resourceMap.put(Constant.MODULE_FRAMEWORK_ID, resMgr);
			}
			{
				ResourceManager resMgr = ResourceManager.getInstance(Constant.MODULE_DCSMAIN_ID, "dcs.resource");
				resourceMap.put(Constant.MODULE_DCSMAIN_ID, resMgr);
			}
			{
				ResourceManager resMgr = ResourceManager.getInstance(Constant.MODULE_SECU_ID, "secu.resource");
				resourceMap.put(Constant.MODULE_SECU_ID, resMgr);
			}
		} catch (ReadResourceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getDcsStr(String key) {
		return getCommStr(Constant.MODULE_DCSMAIN_ID, key);
	}

	/**
	 * 安全模块的定义
	 * 
	 * @param key
	 * @return
	 */
	public String getSecuStr(String key) {
		return getCommStr(Constant.MODULE_SECU_ID, key);
	}

	// public ResourceManager getFrkResManager() {
	// return resourceMap.get(Constant.MODULE_FRAMEWORK_ID);
	// }

	public String getFrkStr(String key) {
		return getCommStr(Constant.MODULE_FRAMEWORK_ID, key);
	}

	public String getCommStr(String moduleId, String key) {
		ResourceManager res = resourceMap.get(moduleId);
		if (res != null) {
			return res.getString(key);
		}
		return "UnDefine";
	}

	public String getCommStr(String moduleId, String key, Object oper) {
		Object[] para = new Object[1];
		para[0] = oper == null ? "" : " [" + oper.toString() + "]";
		return getCommStr(moduleId, key, para);
	}

	public String getCommStr(String moduleId, String key, Object[] para) {
		String tmp = getCommStr(moduleId, key);
		try {
			return String.format(tmp, para);
		} catch (Throwable e) {
			return tmp;
		}
	}
}
